﻿using System;
using Gp.Scripts.Core;
using Gp.Scripts.GUI;
using PixelCrushers.DialogueSystem;
using QFSW.QC;
using UnityEngine;

namespace Gp.Scripts.Core.Misc {
    public class QuestLuaRegister : LuaRegister {
        /// <summary>
        /// 检查任务状态字符串是否合法
        /// </summary>
        /// <param name="stateString"></param>
        /// <returns></returns> 
        private static bool CheckStateString(string stateString) {
            var state = QuestLog.StringToState(stateString);
            if (state != QuestState.Unassigned) return true;


            Debug.LogError("Quest StateString format error: " + stateString);
            return false;
        }

        private static int ToInt(double value) {
            return (int) Math.Round(value);
        }


        private static string WiseGetQuest() {
            var res = Global.Get<ConversationSystem>().ResolveConversation(DialogueManager.Instance.lastConversationStarted)
                .Quest;
            return res;
        }


        /// <summary>
        /// 设置任务状态
        /// </summary>
        [LuaFunction("SetQuest", "SetQst")]
        private static void SetQuestState(string questName, string stateString) {
            if (!CheckStateString(stateString)) return;
            if (stateString.Equals("Active") && QuestLog.GetQuestState(questName) is QuestState.Active or QuestState.Success) {
                return;
            }
            if (stateString.Equals("Success") && QuestLog.GetQuestState(questName) is QuestState.Success) {
                return;
            }
            QuestLog.SetQuestState(questName, stateString);
        }


        /// <summary>
        /// 设置子任务状态
        /// </summary>
        [LuaFunction("SetQE")]
        private static void SetQuestEntryState(string questName, double entryNum, string stateString) {
            if (!CheckStateString(stateString)) return;

            if (entryNum > 6) return;
            QuestLog.SetQuestEntryState(questName, ToInt(entryNum), stateString);
        }


        [LuaFunction("WsSetQuest", "WsSetQst")]
        private static void WiseSetQuestState(string stateString) {
            SetQuestState(WiseGetQuest(), stateString);
        }


        [LuaFunction("WsSetQE")]
        private static void WiseSetQuestEntryState(double entryNum, string stateString) {
            SetQuestEntryState(WiseGetQuest(), entryNum, stateString);
        }


        /// <summary>
        /// 获得当前任务状态
        /// </summary>
        [LuaFunction("GetQuest", "GetQst")]
        private static string GetCurrentQuestState(string questName) {
            var state = QuestLog.GetQuestState(questName);
            return QuestLog.StateToString(state);
        }


        /// 获取子任务状态
        [LuaFunction("GetQE")]
        private static string GetCurrentQuestEntryState(string questName, double entryNum) {
            var state = QuestLog.GetQuestEntryState(questName, ToInt(entryNum));
            return QuestLog.StateToString(state);
        }


        /// <summary>
        /// 智能获取子任务状态。
        /// 智能取当前对话（最后触发的对话）的目录名为父任务名。
        /// </summary>
        /// <param name="entryNum"></param>
        /// <returns></returns>
        [LuaFunction("WsGetQE")]
        private static string WiseGetCurrentQuestEntryState(double entryNum) {
            return GetCurrentQuestEntryState(WiseGetQuest(), entryNum);
        }

        /// <summary>
        /// 智能获取任务状态。
        /// 智能取当前对话（最后触发的对话）的目录名为任务名。
        /// </summary>
        /// <returns></returns>
        [LuaFunction("WsGetQst", "WsGetQuest")]
        private static string WiseGetCurrentQuestState() {
            var title = WiseGetQuest();
            return GetCurrentQuestState(title);
        }


        
        
        
        
        

        /// <summary>
        /// 某个子任务是否处于激活状态
        /// </summary>
        [LuaFunction("IsAtvQE")]
        private static bool IsQuestEntryActive(string questName, double entryNum) {
            // 是否激活
            return QuestLog.GetQuestEntryState(questName, ToInt(entryNum)) == QuestState.Active;
        }
        
        [LuaFunction("IsSucQE")]
        private static bool IsQuestEntrySuccess(string questName, double entryNum) {
            return QuestLog.GetQuestEntryState(questName, ToInt(entryNum)) == QuestState.Success;
        }

        [LuaFunction("HsAtvQE")]
        private static bool HasQuestEntryActive(string questName, double entryNum) {
            return QuestLog.GetQuestEntryState(questName, ToInt(entryNum)) >= QuestState.Active;
        }
        
        [LuaFunction("HsEndQE")]
        private static bool HasQuestEntryEnd(string questName, double entryNum) {
            return QuestLog.GetQuestEntryState(questName, ToInt(entryNum)) > QuestState.Active;
        }

        
        

        [LuaFunction("WsIsAtvQE")]
        private static bool WiseIsQuestEntryActive(double entryNum) {
            return IsQuestEntryActive(WiseGetQuest(), entryNum);
        }

        [LuaFunction("WsIsQuest")]
        private static bool WiseIsQuestEntrySuccess(double entryNum) {
            return IsQuestEntrySuccess(WiseGetQuest(), entryNum);
        }

        [LuaFunction("WsHsAtvQE")]
        private static bool WiseHasQuestEntryActive(double entryNum) {
            return HasQuestEntryActive(WiseGetQuest(), entryNum);
        }
        
        
        [LuaFunction("WsHsEndQE")]
        private static bool WiseHasQuestEntryEnd(double entryNum) {
            return HasQuestEntryEnd(WiseGetQuest(), entryNum);
        }
        
        
        
        
        
        
        
        /// <summary>
        /// 激活某个子任务
        /// </summary>
        [LuaFunction("AtvQE", "ActiveQE")]
        private static void ActiveQuestEntry(string questName, double entryNum) {
            // 如果该任务已经激活过，则无效
            if (HasQuestEntryActive(questName, entryNum)) return;
            
            QuestLog.SetQuestEntryState(questName, ToInt(entryNum), QuestState.Active);
        }

        
        [LuaFunction("SucQE")]
        private static void SucQuestEntry(string questName, double entryNum) {
            // 只有处于激活状态的任务能完成
            if (!IsQuestEntryActive(questName, entryNum)) return;
            
            QuestLog.SetQuestEntryState(questName, ToInt(entryNum), QuestState.Success);
        }

        
        [LuaFunction("WsSucQE")]
        private static void WsSucQuestEntry(double entryNum) {
            SucQuestEntry(WiseGetQuest(), entryNum);
        }

        
        [LuaFunction("WsAtvQE")]
        private static void WsAtvQuestEntry(double entryNum) {
            ActiveQuestEntry(WiseGetQuest(), entryNum);
        }
        
    }
}